#include <stdio.h>
#include <stdlib.h>
#include "mod_logger.h"
#include <stdarg.h>

#define LOG_LEVEL 3
#define LINFO 1
#define LVERBOSE 2
#define LDEBUG 3
#define LOG_FILE "log_file.log"


void logger(const int flag, const char *msg, ...)
{
        char *cflag;
        FILE *logfile;
        va_list va;

        if(flag > LOG_LEVEL)
                return;
        switch(flag)
        {
        case LINFO:
                cflag = "INFO: ";
                break;
        case LVERBOSE:
                cflag = "VERBOSE: ";
                break;
        case LDEBUG:
                cflag = "DEBUG: ";
                break;
        default:
                fprintf(stderr, "Error no flag specified!!\n");
                exit(1);
        }
        logfile = fopen(LOG_FILE, "a+");
        if(logfile == NULL)
        {
                perror("Error on open log file");
                exit(1);
        }
        fprintf(logfile, "%lu ", time(NULL));
        fprintf(logfile, "%s", cflag);
        fprintf(logfile, "[%d] ", getpid());
        va_start(va, msg);
        vfprintf(logfile, msg, va);
        va_end(va);
        fprintf(logfile, "\n");
        fflush(logfile);
        fclose(logfile);
}

